package com.intexh.quickandroid.module.chat.bean;

import com.intexh.quickandroid.module.chat.sprinkles.ManyQuery;
import com.intexh.quickandroid.module.chat.sprinkles.Model;
import com.intexh.quickandroid.module.chat.sprinkles.OneQuery;
import com.intexh.quickandroid.module.chat.sprinkles.Query;
import com.intexh.quickandroid.module.chat.sprinkles.annotations.Column;
import com.intexh.quickandroid.module.chat.sprinkles.annotations.PrimaryKey;
import com.intexh.quickandroid.module.chat.sprinkles.annotations.Table;
import com.intexh.quickandroid.utils.GsonUtils;

import java.util.List;


@Table("ChatSession")
public class ChatSession extends Model {

//    public static long nearFieldChatId = 100000001;
    @PrimaryKey
    @Column("sessionId")
    private String sessionId;
    @Column("lastMessage")
    private String lastMessage;
    @Column("notReadCount")
    private int notReadCount;
    @Column("time")
    private Long time;
    @Column("isGroup")
    private boolean isGroup;
    @Column("isNearField")
    private boolean isNearField;
    @Column("uid")
    private String uid;	//uid
    @Column("class_id")
    private String class_id;	//uid
    @Column("avatar")
    private String avatar;
    @Column("name")
    private String name;
    @Column("gender")
    private int gender;
    @Column("tags")
    private String tags;
    @Column("isOnNFChat")
    private boolean isOnNFChat;
    @Column("groupTargetId")
    private long groupTargetId;
    @Column("group_member_count")
    private int group_member_count;
    @Column("is_vip")
    private int is_vip;	//是否是vip,1是0否
    @Column("relation")
    private Integer relation = -1;	//0陌生人，1已发送好友请求，2好友，3用户自己，4:对方已发送好友请求
    @Column("createTime")
    private long createTime;


    @Column("group_id")			//群id
    private String group_id;
    @Column("group_name")			//群名称
    private String group_name;
    @Column("group_avatar")			//群名称
    private String group_avatar;    //群头像


    public String getClass_id() {
        return class_id;
    }

    public void setClass_id(String class_id) {
        this.class_id = class_id;
    }

    public String getGroup_id() {
        return group_id;
    }

    public void setGroup_id(String group_id) {
        this.group_id = group_id;
    }

    public String getGroup_name() {
        return group_name;
    }

    public void setGroup_name(String group_name) {
        this.group_name = group_name;
    }

    public String getGroup_avatar() {
        return group_avatar;
    }

    public void setGroup_avatar(String group_avatar) {
        this.group_avatar = group_avatar;
    }

    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public int getIs_vip() {
        return is_vip;
    }

    public void setIs_vip(int is_vip) {
        this.is_vip = is_vip;
    }

    public Integer getRelation() {
        return relation;
    }

    public void setRelation(Integer relation) {
        this.relation = relation;
    }

    public String getTags() {
        return tags;
    }

    public void setTags(String tags) {
        this.tags = tags;
    }

    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public boolean isNearField() {
		return isNearField;
	}

	public void setNearField(boolean isNearField) {
		this.isNearField = isNearField;
	}

	private String searchMessage;
    @Column("emConnversationName")
    String emConnversationName;
    

	public String getEmConnversationName() {
		return emConnversationName;
	}

	public void setEmConnversationName(String emConnversationName) {
		this.emConnversationName = emConnversationName;
	}

	public String getSearchMessage() {
		return searchMessage;
	}

	public void setSearchMessage(String searchMessage) {
		this.searchMessage = searchMessage;
	}

	public ChatSession() {
    }

    public static void deleteAll(){
        List<ChatSession> list = ChatSession.getAll();
        for(ChatSession m:list){
            m.deleteAsync();
        }
    }

    public static ChatSession getSession(String sessionId) {
        OneQuery<ChatSession> query = Query.one(ChatSession.class, "select * from ChatSession where sessionId=?", sessionId);
        if(query != null){
            return query.get();
        }
        return null;
    }

    public static ChatSession update(String sessionId) {
        OneQuery<ChatSession> query =  Query.one(ChatSession.class, "update ChatSession SET notReadCount=? where sessionId=?",0,sessionId);
        if(query != null){
            return query.get();
        }
        return null;
    }
    public static ChatSession deleteSession(String sessionId) {
        OneQuery<ChatSession> query = Query.one(ChatSession.class, "delete from ChatSession where sessionId=?", sessionId);
        if(query != null){
            return query.get();
        }
        return null;
    }
    
    public static List<ChatSession> getAll() {
            ManyQuery<ChatSession> query = Query.many(ChatSession.class, "select * from ChatSession order by createTime desc");
            if (query != null){
                return query.get().asList();
            }

        return null;
    }

    public static List<ChatSession> getAllIncludeNF() {
        ManyQuery<ChatSession> query = Query.many(ChatSession.class, "select * from ChatSession order by time desc");
        if (query != null){
            return query.get().asList();
        }
        return null;
    }
   public static int getAllUnread() {
        int  count = Query.count("select sum(notReadCount) from ChatSession");
        return count;
    }

    public String getSessionId() {
		return sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

    public String getLastMessage() {
        return lastMessage;
    }

    public void setLastMessage(String lastMessage) {
        this.lastMessage = lastMessage;
    }

    public int getNotReadCount() {
        return notReadCount;
    }

    public void setNotReadCount(int notReadCount) {
        this.notReadCount = notReadCount;
    }

    public Long getTime() {
        return time;
    }

    public void setTime(Long time) {
        this.time = time;
    }

    public boolean getIsGroup() {
        return isGroup;
    }

    public void setIsGroup(boolean isGroup) {
        this.isGroup = isGroup;
    }



    public boolean isOnNFChat() {
        return isOnNFChat;
    }

    public void setIsOnNFChat(boolean isOnNFChat) {
        this.isOnNFChat = isOnNFChat;
    }

    public long getGroupTargetId() {
        return groupTargetId;
    }

    public void setGroupTargetId(long groupTargetId) {
        this.groupTargetId = groupTargetId;
    }

    public int getGroup_member_count() {
        return group_member_count;
    }

    public void setGroup_member_count(int group_member_count) {
        this.group_member_count = group_member_count;
    }

    @Override
    public String toString() {
        return GsonUtils.serializedToJson(this);
    }
}
